//	MEP Divenav
//	(c) FH Aachen 2009
//
//	file: 		spi.c
//	purpose:	define interface for SPI communication of AT90CAN128 on
//				Display 3000 D072
//	uses:

#include <avr/wdt.h>
#include <stdio.h>
#include <avr/io.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <string.h>
#include "../global_types.h"
#include "spidummy.h"

//*********************************************************
// SPI routine to write one byte
//*********************************************************
void SpiWriteByte (uint8_t usData)
{
    SPDR = usData;
	while(!(SPSR & (1<<SPIF)));
	//ignore reply
}


//*********************************************************
// SPI routine to write one word (two bytes)
//*********************************************************
void  SpiWriteWord(uint16_t Data)
{
	uint16_t msbyte;
	uint16_t lsbyte;
	msbyte = ((Data) >> 8) & 0xff;		// msb!
	lsbyte =  (Data) & 0xff;			// lsb!
    SPDR = msbyte;
	while(!(SPSR & (1<<SPIF)));		   //Wait until all bits has been sent
    SPDR = lsbyte;
	while(!(SPSR & (1<<SPIF)));
	
}


//*********************************************************
// SPI routine to write two words (four bytes)
//*********************************************************
void SpiWrite2Words (uint32_t Data){
	int i;
	uint8_t mmsbyte;
	uint8_t msbyte;
	uint8_t lsbyte;
	uint8_t llsbyte;

	mmsbyte = ((Data) >> 24) & 0xffff;		// msb!
	msbyte = ((Data) >> 16) & 0xffff;
	lsbyte = ((Data) >> 8) & 0xffff;
	llsbyte =  (Data) & 0xffff;
    
	SPDR = mmsbyte;
	while(!(SPSR & (1<<SPIF)));	//Wait until all bits has been sent
    
	SPDR = msbyte;
	while(!(SPSR & (1<<SPIF)));
	
	SPDR = lsbyte;
	while(!(SPSR & (1<<SPIF)));		      //Wait until all bits has been sent
	
	SPDR = llsbyte;
	while(!(SPSR & (1<<SPIF)));		      //Wait until all bits has been sent
	
}

//*************************************************************
// Spi Routine to read a Byte
//*************************************************************
uint8_t SpiReadByte ()
{
	uint8_t rc;
	SPDR = 0;	//arbitary value
	while(!(SPSR & (1<<SPIF))){};
	rc = SPDR;
	return(rc);
}


//*****************************************************************
// Spi Routine to read a word (two bytes)
//*****************************************************************
uint16_t SpiReadWord ()
{
    bytes16bits value;
	value.val=0;
	bytes16bits rxData;
	rxData.val = value.val;
	rxData.val = ((value.val|SpiReadByte())<<8);	//send MSB
	rxData.val |= (value.val|SpiReadByte());	//send LSB
	return rxData.val;					//return 16 bits result
}


//*****************************************************************
// Spi Routine to read two words (four bytes)
//*****************************************************************
bytes32bits SpiRead2Words()
{
	bytes32bits value;
	value.val=0;
	bytes32bits rxData =value;
	rxData.val =  ((value.val|SpiReadByte())<<24);
	rxData.val |= ((value.val|SpiReadByte())<<16);
	rxData.val |= ((value.val|SpiReadByte())<<8);
	rxData.val |=  (value.val|SpiReadByte());
	return rxData;					//return 32 bits result
}


/*
//Spi Routine to read a Byte//
short SpiReadByte ()
{
	SPDR = 0b00000000;//arbitary value
	//loop_until_bit_is_set(SPSR,SPIF);
	return SPDR;					//return received data
}

//Send Routine to read word by SPI//
bytes16bits SpiReadWord ()			      //now*pusData is 16 bits in size
{
	bytes16bits value;
	value.val=0;
	bytes16bits rxData = value;			//local variable
	rxData.val = (SpiReadByte()<<8);//send MSB
	rxData.val |= (SpiReadByte());//send LSB
	return rxData;					//return 16 bits result
}


short SpiRead4Bytes ()
{
	long int  rxData = 0;				//local variable
	rxData = (SpiReadByte()<<8);//send first Byte
	rxData |= (SpiReadByte()<<8);//send Second Byte
	rxData |= (SpiReadByte()<<8);//send Third Byte
    rxData |= (SpiReadByte()<<8);//send Last Byte
	return rxData;					//return 16 bits result
}
*/

/*
void SpiWrite2Words (bytes32bits * p4bData){
	int i;

	for (i=0;i<4;i++){
	    SPDR = p4bData->a[i];
		while(!(SPSR & (1<<SPIF)));		      //Wait until all bits has been sent
	}
}
*/
